package com.cz.modules.gc.service.impl;

import com.cz.commons.dynamic.datasource.annotation.DataSource;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cz.common.utils.PageUtils;
import com.cz.common.utils.Query;

import com.cz.modules.gc.dao.StatoiletseatDao;
import com.cz.modules.gc.entity.StatoiletseatEntity;
import com.cz.modules.gc.service.StatoiletseatService;
import org.springframework.transaction.annotation.Transactional;


@Service("statoiletseatService")
@Transactional
@DataSource("slave1")
public class StatoiletseatServiceImpl extends ServiceImpl<StatoiletseatDao, StatoiletseatEntity> implements StatoiletseatService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<StatoiletseatEntity> page = this.page(
                new Query<StatoiletseatEntity>().getPage(params),
                new QueryWrapper<StatoiletseatEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Map<String, Object> getToiletSeat() {
        Calendar c=Calendar.getInstance();
        c.add(Calendar.MINUTE,-10);
        List<StatoiletseatEntity> seatList=this.list(
                new QueryWrapper<StatoiletseatEntity>()
                        .ge("validdatetime",c.getTime())
                        .orderByDesc("validdatetime")
        );
        Map<String,String> southMale=new HashMap<>();
        Map<String,String> southFemale=new HashMap<>();
        Map<String,String> northMale=new HashMap<>();
        Map<String,String> northFemale=new HashMap<>();
        for (StatoiletseatEntity seat:seatList) {
            //南区男厕001
            //南区女厕002
            //北区男厕003
            //北区女厕004
            if(seat.getToiletcode().equals("001")){
                if(!southMale.containsKey(seat.getPnum())){
                    southMale.put(seat.getPnum(),seat.getPvalue());
                }
            }else if(seat.getToiletcode().equals("002")){
                if(!southFemale.containsKey(seat.getPnum())){
                    southFemale.put(seat.getPnum(),seat.getPvalue());
                }
            }else if(seat.getToiletcode().equals("003")){
                if(!northMale.containsKey(seat.getPnum())){
                    northMale.put(seat.getPnum(),seat.getPvalue());
                }
            }else if(seat.getToiletcode().equals("004")){
                if(!northFemale.containsKey(seat.getPnum())){
                    northFemale.put(seat.getPnum(),seat.getPvalue());
                }
            }
        }
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("southMale",southMale);
        resultMap.put("southFemale",southFemale);
        resultMap.put("northMale",northMale);
        resultMap.put("northFemale",northFemale);
        return resultMap;
    }

    @Override
    public Map<String, Object> checkEmptycw() {
        Calendar c=Calendar.getInstance();
        c.add(Calendar.MINUTE,-10);
        List<StatoiletseatEntity> seatList=this.list(
                new QueryWrapper<StatoiletseatEntity>()
                        .ge("validdatetime",c.getTime())
                        .orderByDesc("validdatetime")
        );
        Map<String,String> southMale=new HashMap<>();
        Map<String,String> southFemale=new HashMap<>();
        Map<String,String> northMale=new HashMap<>();
        Map<String,String> northFemale=new HashMap<>();
        Integer southMaleUsed=0;
        Integer southFemaleUsed=0;
        Integer northMaleUsed=0;
        Integer northFemaleUsed=0;
        for (StatoiletseatEntity seat:seatList) {
            //南区男厕001
            //南区女厕002
            //北区男厕003
            //北区女厕004
            if(seat.getToiletcode().equals("001")){
                if(!southMale.containsKey(seat.getPnum())){
                    southMale.put(seat.getPnum(),seat.getPvalue());
                    if(seat.getPvalue().equals("1")){
                        southMaleUsed++;
                    }
                }
            }else if(seat.getToiletcode().equals("002")){
                if(!southFemale.containsKey(seat.getPnum())){
                    southFemale.put(seat.getPnum(),seat.getPvalue());
                    if(seat.getPvalue().equals("1")){
                        southFemaleUsed++;
                    }
                }
            }else if(seat.getToiletcode().equals("003")){
                if(!northMale.containsKey(seat.getPnum())){
                    northMale.put(seat.getPnum(),seat.getPvalue());
                    if(seat.getPvalue().equals("1")){
                        northMaleUsed++;
                    }
                }
            }else if(seat.getToiletcode().equals("004")){
                if(!northFemale.containsKey(seat.getPnum())){
                    northFemale.put(seat.getPnum(),seat.getPvalue());
                    if(seat.getPvalue().equals("1")){
                        northFemaleUsed++;
                    }
                }
            }
        }
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("southMaleUsed",southMaleUsed);
        resultMap.put("southFemaleUsed",southFemaleUsed);
        resultMap.put("northMaleUsed",northMaleUsed);
        resultMap.put("northFemaleUsed",northFemaleUsed);
        return resultMap;
    }

}
